package com.isoft.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * @author LiuGaoMing
 * @version 1.0
 * @description:
 * @date 2024/5/16 10:31
 */
@Controller
public class FileController {

    @GetMapping("/userLogin")
    public String toLoginPage(){
        return "login";
    }

    @GetMapping("/movie/{type}/{path}")
    public String toMovie(@PathVariable("type") String type,@PathVariable("path") String path){
        return "movie/"+type+"/"+path;
    }

//    @GetMapping("/getUserBySession")
//    @ResponseBody
//    public void getUser(HttpSession session){
//        Enumeration<String> attributeNames = session.getAttributeNames();
//        while (attributeNames.hasMoreElements()){
//            String element = attributeNames.nextElement();
//            SecurityContextImpl attribute = (SecurityContextImpl) session.getAttribute(element);
////            System.out.println("element = " + element);
////            System.out.println("attribute = " + attribute);
//            Authentication authentication = attribute.getAuthentication();
//            UserDetails details = (UserDetails) authentication.getPrincipal();
//            String username = details.getUsername();
//        }
//    }

//    @GetMapping("/")
//    public String getUser(Model model, HttpSession session) {
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        if (authentication != null && authentication.isAuthenticated()) {
//            UserDetails details = (UserDetails) authentication.getPrincipal();
//            String username = details.getUsername();
//            model.addAttribute("name", username);
//        }
//        return "index";
//    }

    @GetMapping("/getUserByContext")
    public void getUser1(){
        SecurityContext context = SecurityContextHolder.getContext();
        System.out.println("context = " + context);
        Authentication authentication = context.getAuthentication();
        UserDetails details = (UserDetails) authentication.getPrincipal();
        System.out.println("details = " + details);
        String username = details.getUsername();
        System.out.println("username = " + username);

    }

}
